<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增诊疗记录')" />
    <th:block th:include="include :: select2-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-records-add">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">选择宠物：</label>
                    <div class="col-sm-8">
                        <select name="petId" id="petSelect" class="form-control m-b">
                            <!-- 这里将通过 JavaScript 动态填充宠物选项 -->
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">诊断结果：</label>
                    <div class="col-sm-8">
                        <textarea name="diagnosis" class="form-control"></textarea>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: select2-js" />
    <script th:inline="javascript">
        // console.log("-----");
        // console.log(document.cookie);
        var prefix = ctx + "system/records"
        $("#form-records-add").validate({
            focusCleanup: true
        });
        function submitHandler() {
            console.log(document.cookie)
            if ($.validate.form()) {
                // console.log(document.cookie)
                // console.log($('#form-records-add').serialize())
                $.operate.save(prefix + "/add", $('#form-records-add').serialize());
            }
        }

        $(document).ready(function() {
            // 封装一个通用的函数，用于获取数据并填充到指定的 select 元素中
            function loadDataAndInitSelect(url, selectId, textField, valueField) {
                $.ajax({
                    url: url,
                    type: 'POST',
                    dataType: 'json',
                    success: function(res) {
                        // console.log("--------------")
                        // console.log(res)
                        var dataList = res.rows;
                        var selectElement = $(selectId);

                        // 清空原有的选项
                        selectElement.empty();

                        // 动态添加选项
                        $.each(dataList, function(index, item) {
                            // console.log(index)
                            // console.log(item)
                            // console.log(item.textField)
                            // console.log(item[textField])
                            var option = $('<option></option>');
                            // 使用方括号语法获取属性值
                            var displayText = item[textField];
                            var optionValue = item[valueField];

                            // 检查获取的值是否有效
                            if (displayText !== undefined && optionValue !== undefined) {
                                option.text(displayText);
                                option.val(optionValue);
                                selectElement.append(option);
                            } else {
                                console.log('未能获取到有效的属性值，textField:', textField, 'valueField:', valueField);
                            }
                        });

                        // 初始化 Select2 插件，设置 placeholder 并清除默认选择
                        selectElement.select2({
                            placeholder: '请选择',  // 设置占位符文本
                            allowClear: true        // 允许清除选择
                        }).val(null).trigger('change');  // 清除默认选择并触发 change 事件
                    },
                    error: function(error) {
                        console.log('请求出错:', error);
                    }
                });
            }

            // 加载宠物列表
            loadDataAndInitSelect('/system/pets/list', '#petSelect', 'name', 'petId');
        });
    </script>
</body>
</html>